package com.sduept.nwld.dataserver.interceptors;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.sduept.cim.model.controlarea.HostControlArea;
import com.sduept.cim.model.core.BasePower;
import com.sduept.cim.model.core.BaseVoltage;
import com.sduept.cim.model.core.DCSys;
import com.sduept.cim.model.core.Equipment;
import com.sduept.cim.model.core.SubControlArea;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.core.VoltageLevel;
import com.sduept.cim.model.generation.GeneratingUnit;
import com.sduept.cim.model.generation.HydroGeneratingUnit;
import com.sduept.cim.model.generation.ThermalGeneratingUnit;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.cim.model.wires.Breaker;
import com.sduept.cim.model.wires.BusbarSection;
import com.sduept.cim.model.wires.Compensator;
import com.sduept.cim.model.wires.DCPole;
import com.sduept.cim.model.wires.DCSwitch;
import com.sduept.cim.model.wires.DcLinesegment;
import com.sduept.cim.model.wires.Disconnector;
import com.sduept.cim.model.wires.EnergyConsumer;
import com.sduept.cim.model.wires.GroundDisconnector;
import com.sduept.cim.model.wires.PowerTransformer;
import com.sduept.cim.model.wires.RectifierInverter;
import com.sduept.cim.model.wires.SynchronousMachine;
import com.sduept.cim.model.wires.TapChanger;
import com.sduept.cim.model.wires.TransformerWinding;
import com.sduept.component.message.model.CimEquipmentUpdateMessage;
import com.sduept.component.message.model.EquipmentUpdateMessage;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.enums.EquipmentUpdateEnum;

@Aspect
@Service
public class CimEquipmentUpdateInterceptor {

	@Autowired
	private SuperDAO superDao;


	@Pointcut("execution(* com.sduept.component.message.core.CimEuipmentUpdateService.updateCimModelByMessage(..))")
	public void declareJoinPointExpression(){

	}

	@Around(value = "declareJoinPointExpression()")
	public Object methodInterceptor(ProceedingJoinPoint ctx) throws Exception {
		
		String methodName = ctx.getSignature().getName();//方法名
		Object[] parameter = ctx.getArgs();//参数
		Object proceed = null;
		try {
			proceed = ctx.proceed();
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		if("updateCimModelByMessage".equals(methodName)){

			EquipmentUpdateMessage message = (EquipmentUpdateMessage) parameter[0];
			EquipmentUpdateEnum equipmentUpdateEnum = EquipmentUpdateEnum.val(message.getOperateObject());
			switch (equipmentUpdateEnum) {
				case PRIMARY_ACLINESEGMENT:
					handleEquipment(ACLineSegment.class,message);
					break;
				case PRIMARY_BASEPOWER:
					handleEquipment(BasePower.class,message);
					break;
				case PRIMARY_BASEVOLTAGE:
					handleEquipment(BaseVoltage.class,message);
					break;
				case PRIMARY_BREAKER:
					handleEquipment(Breaker.class,message);
					break;
				case PRIMARY_BUSBARSECTION:
					handleEquipment(BusbarSection.class,message);
					break;
				case PRIMARY_COMPENSATOR:
					handleEquipment(Compensator.class,message);
					break;
				case PRIMARY_DCLINESEGMENT:
					handleEquipment(DcLinesegment.class,message);
					break;
				case PRIMARY_DCPOLE:
					handleEquipment(DCPole.class,message);
					break;
				case PRIMARY_DCSWITCH:
					handleEquipment(DCSwitch.class,message);
					break;
				case PRIMARY_DCSYS:
					handleEquipment(DCSys.class,message);
					break;
				case PRIMARY_DISCONNECTOR:
					handleEquipment(Disconnector.class,message);
					break;
				case PRIMARY_ENERGYCONSUMER:
					handleEquipment(EnergyConsumer.class,message);
					break;
				case PRIMARY_GENERATINGUNIT:
					handleEquipment(GeneratingUnit.class,message);
					break;
				case PRIMARY_GROUNDDISCONNECTOR:
					handleEquipment(GroundDisconnector.class,message);
					break;
				case PRIMARY_HOSTCONTROLAREA:
					handleEquipment(HostControlArea.class,message);
					break;
				case PRIMARY_HYDROGENERATINGUNIT:
					handleEquipment(HydroGeneratingUnit.class,message);
					break;
				case PRIMARY_POWERTRANSFORMER:
					handleEquipment(PowerTransformer.class,message);
					break;
				case PRIMARY_SUBCONTROLAREA:
					handleEquipment(SubControlArea.class,message);
					break;
				case PRIMARY_SUBSTATION:
					handleEquipment(Substation.class,message);
					break;
				case PRIMARY_RECTIFIERINVERTER:
					handleEquipment(RectifierInverter.class,message);
					break;
				case PRIMARY_SYNCHRONOUSMACHINE:
					handleEquipment(SynchronousMachine.class,message);
					break;
				case PRIMARY_TAPCHANGER:
					handleEquipment(TapChanger.class,message);
					break;
				case PRIMARY_THERMALGENERATINGUNIT:
					handleEquipment(ThermalGeneratingUnit.class,message);
					break;
				case PRIMARY_TRANSFORMERWINDING:
					handleEquipment(TransformerWinding.class,message);
					break;
				case PRIMARY_VOLTAGELEVEL:
					handleEquipment(VoltageLevel.class,message);
					break;
//				case PRIMARY_INCREMENTAL_UPDATE_END:
//					handleConnectivityNode();
//					handleTerminals();
//					break;
				default:
					break;
			}
		}
		
		return proceed;
	}
	
	private <T> void handleEquipment(Class<T> cla,EquipmentUpdateMessage message){
		CimEquipmentUpdateMessage ceum = new CimEquipmentUpdateMessage();
		T target = JSON.parseObject(message.getMessageContent(), cla);
		Equipment bean=(Equipment)target;
		EquipmentUpdateEnum equipmentUpdateEnum = EquipmentUpdateEnum.val(message.getOperateType());
		switch (equipmentUpdateEnum) {
			case OPERATE_CREATE:
				ceum.setName("新增" + bean.getName());
				ceum.setContent("新增" + bean.getName() + "，id为" + bean.getId() + "，更新时间为" + message.getUpdateTime());
				ceum.setOperation(0);
				ceum.setStatus(0);
				ceum.setUpdateTime(message.getUpdateTime());
				ceum.setIsAlarm(0);
				break;
			case OPERATE_UPDATE:
				ceum.setName("更新" + bean.getName());
				ceum.setContent("更新" + bean.getName() + "，id为" + bean.getId() + "，更新时间为" + message.getUpdateTime());
				ceum.setOperation(1);
				ceum.setStatus(0);
				ceum.setUpdateTime(message.getUpdateTime());
				ceum.setIsAlarm(0);
				break;
			case OPERATE_DELETE:
				ceum.setName("删除" + bean.getName());
				ceum.setContent("删除" + bean.getName() + "，id为" + bean.getId() + "，删除时间为" + message.getUpdateTime());
				ceum.setOperation(2);
				ceum.setStatus(0);
				ceum.setUpdateTime(message.getUpdateTime());
				ceum.setIsAlarm(0);
				break;
			default:
				break;
		}
		superDao.create(ceum, CimEquipmentUpdateMessage.class);
	}
}
